import { useEffect, useMemo, useState } from 'react';
import { Button, Spin, Tabs, message } from 'antd';
import { ReloadOutlined } from '@ant-design/icons';
import Cls from 'classnames';

import '../common/styles.less';
import pageStyles from '@/common/pageLayout.less';
import { handlCheckManageService } from '../services';
import { getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { CalibrateHomeCheckPane } from './CalibrateHomeCheckPane';
export const CalibrateHomeView: React.FC<CalibrateHomeAPI.ICalibrateHomeView> = ({ curRes }) => {
  /** 样式根目录 */
  const styleFixCls = 'calibrate-homesty';

  /** 保存合同数据 */
  const [manageDataList, setManageDataList] = useState<Record<string, any>[]>([]);

  /** 回调 */
  const homeCallback = (res: any) => {
    if (res && res?.msgFlag) {
      setManageDataList(res?.rows);
    } else {
      message.error(res?.msg);
    }
  };

  /** 获取数据 */
  // const { loading, reloadRefresh, getContractData } = contractHomeService(curRes, homeCallback);

  /** 获取代办已办数据 */
  const { taskLoading, getTaskCheckData, reloadTaskData } = handlCheckManageService(curRes, homeCallback);

  /** 存入选中的tab标识 */
  const [tabPaneKey, setTabPaneKey] = useState<string>('1');

  /** 返回子组件 */
  const renderTabsPane = () => {
    return <CalibrateHomeCheckPane styleClassName={styleFixCls} calibrateInfoList={manageDataList} />;
    // if (tabPaneKey == '1' || tabPaneKey == '4') {
    //   return <ContractHomeCheckPane styleClassName={styleFixCls} contractInfoList={manageDataList} />;
    // } else {
    //   return <ContractHomeTabsPane styleClassName={styleFixCls} contractInfoList={manageDataList} />;
    // }
  };

  /** 刷新回调 */
  const reloadHomeCallback = () => {
    reloadTaskData();
    // if (tabPaneKey == '1' || tabPaneKey == '4') {
    //   reloadTaskData();
    // } else {
    //   reloadRefresh();
    // }
  };

  /** 刷新 */
  const tabsExtraBtns = () => {
    return <Button type="primary" icon={<ReloadOutlined />} onClick={reloadHomeCallback} />;
  };

  /** tabs子页面 */
  const tabsItem = useMemo(() => {
    let items: any[] = [
      {
        label: '我的采购定标待办',
        key: '1',
        children: renderTabsPane(),
        isShow: curRes['/purchaseSourceCalibrateController/dealHomePage'] || false,
      },
      {
        label: '我的采购定标已办',
        key: '4',
        children: renderTabsPane(),
        isShow: curRes['/purchaseSourceCalibrateController/dealedHomePage'] || false,
      },
      {
        label: '我的采购定标退回',
        key: '2',
        children: renderTabsPane(),
        isShow: curRes['/purchaseSourceCalibrateController/returnHomePage'] || false,
      },
      {
        label: '我的采购定标起草',
        key: '3',
        children: renderTabsPane(),
        isShow: curRes['/purchaseSourceCalibrateController/dealHomePage'] || false,
      },
    ];

    const newItems: any[] = items?.filter((item: any) => item?.isShow);
    return newItems;
  }, [manageDataList, tabPaneKey, curRes]);

  useEffect(() => {
    if (tabPaneKey == '1') {
      getTaskCheckData({
        taskType: false,
      });
    } else if (tabPaneKey == '2') {
      getTaskCheckData({
        // state: `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1003`
        checkState: `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1001`,
      });
    } else if (tabPaneKey == '3') {
      getTaskCheckData({
        state: `${getTenantID()}${ILoadMapTypeAll.CALIBRATE_STATE}1000`,
      });
    } else if (tabPaneKey == '4') {
      getTaskCheckData({
        taskType: true,
      });
    }
  }, [tabPaneKey]);

  return (
    <div className={Cls(`${styleFixCls}`, pageStyles['commonTabsLine'])}>
      <Spin spinning={taskLoading}>
        <Tabs
          activeKey={tabPaneKey}
          tabBarExtraContent={tabsExtraBtns()}
          items={tabsItem}
          onChange={(activeKey: string) => {
            setTabPaneKey(activeKey);
          }}
        />
      </Spin>
    </div>
  );
};
